package com.baohua.controller;

import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baohua.exception.BaohuaSystemException;
import com.baohua.model.Supplier;
import com.baohua.service.SupplierService;

@Controller
@RequestMapping("/supplier")
public class SupplierController {
 
	/**
	 * 跳转到供应商主页面
	 * @return
	 */
	@RequestMapping("/index")
	public String index() {
		return "supplier";
	}
	
	/**
	 * 根据条件查询供应商列表
	 * @param request
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping("/list")
	public @ResponseBody Map queryList(Supplier supplier, HttpServletRequest request){
		int page = Integer.parseInt(request.getParameter("page"));
		int pageSize = Integer.parseInt(request.getParameter("pagesize"));
		
		Map map = supplierService.querySupplier(supplier, page, pageSize);
		
		return map;
	}
	
	@RequestMapping("/add")
	public String addSupplier(Supplier supplier, Model model) {
     try{
    	 supplierService.addSupplier(supplier);
	  } catch (BaohuaSystemException e) {
	 		logger.info(e.getMessage());
	 		model.addAttribute("errorMessage", e.getMessage());
	 		return "forward:/system/error";
	  } 
	return "redirect:/supplier/index";
	}
	
	@RequestMapping("/update")
	public String updateSupplier(Supplier supplier, Model model) {
     try{
    	 supplierService.updateSupplier(supplier);
	  } catch (BaohuaSystemException e) {
	 		logger.info(e.getMessage());
	 		model.addAttribute("errorMessage", e.getMessage());
	 		return "forward:/system/error";
	  } 
	return "redirect:/supplier/index";
	}
	
	@RequestMapping(value="/del/{rowsNum}", method=RequestMethod.GET)
	public @ResponseBody String delSupplier(@PathVariable String rowsNum) {
// 		String s = request.getParameter("rowsNum");
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			String[] supplierIds = objectMapper.readValue(rowsNum, String[].class);
			for(String staffId : supplierIds) {
				supplierService.delSupplier(Integer.parseInt(staffId));
			}
		} catch (JsonParseException e) {
			logger.info(e.getMessage());
			return "{message:systemerror}";
		} catch (JsonMappingException e) {
			logger.info(e.getMessage());
			return "{message:'systemerror'}";
		} catch (IOException e) {
			logger.info(e.getMessage());
			return "{message:'systemerror'}";
		} catch (NumberFormatException e) {
			logger.info(e.getMessage());
			return "{message:'systemerror'}";
		} catch (BaohuaSystemException e) {
			logger.info(e.getMessage());
			return "{message:'error'}";
		}
		return "{message : 'success'}";
	}
	
	private SupplierService supplierService;
	private static final Logger logger = Logger.getLogger(SupplierController.class.getName());

	public SupplierService getSupplierService() {
		return supplierService;
	}

	@Resource(name="supplierService")
	public void setSupplierService(SupplierService supplierService) {
		this.supplierService = supplierService;
	}
	
	
}
